package com.jc.service.appgame;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.jc.common.util.Base64Util;
import com.jc.common.util.HttpUtil;
import com.jc.common.util.JSONUtil;
import com.jc.common.util.MD5;
import com.jc.ds.base.service.BaseServiceException;
import com.jc.framework.enums.Conclusion;
import com.jc.framework.enums.LogType;
import com.jc.service.api.appgame.model.NoticUser;
import com.jc.service.apilog.ApiLogService;

@Service
public class AppGameServiceImpl implements AppGameService {
	private final Logger LOGGER = LoggerFactory.getLogger(AppGameServiceImpl.class);
	
	@Value("${appgame.sys.url}")
	private String appgame_url;
	
	@Value("${appgame.sys.totalurl}")
	private String appgame_total_url;
	
	@Value("${appgame.sys.key}")
	private String appgame_key;
	
	@Autowired
	private ApiLogService apiLogService;
	
	
	public JSONObject noticAppGame(NoticUser user) {
		String ret = null;
		JSONObject retObject = new JSONObject();
		if(user == null) {
			throw new BaseServiceException("通知APP游戏错误, 通知参数不能为空") ;
		}
		
		String _data = Base64Util.encode(JSONObject.toJSONString(user));
		String sign = MD5.passmd5("data=" + _data + "&key=" + appgame_key);
		
		Map<String,Object> parMap = new HashMap<String,Object>();
		parMap.put("data", _data);
		parMap.put("sign", sign);
		try {
			ret = HttpUtil.post(appgame_url, parMap);
			if(StringUtils.isBlank(ret)) {
				throw new BaseServiceException("错误, APPGAME接口返回值为空");
			}
			if(!JSONUtil.isJson(ret)) {
				throw new BaseServiceException("错误, APPGAME接口返回值格式不是一个有效的JSON");
			}
			apiLogService.addApiLog(LogType.NOTIC_APP_GAME, JSONObject.toJSONString(parMap), Conclusion.OK, "", ret);
			return JSONObject.parseObject(ret);
		}catch (Exception e) {
			LOGGER.error("请求APP游戏服务器发生错误， 详情: {} , {}" , e.getMessage() , e);
			String errorMsg = e.getMessage();
			if(StringUtils.isNotBlank(e.getMessage()) && e.getMessage().length() > 150) {
				errorMsg = StringUtils.substring(e.getMessage(), 0, 150);
			}
			retObject.put("code", "9999");
			retObject.put("msg", errorMsg);
			apiLogService.addApiLog(LogType.NOTIC_APP_GAME, JSONObject.toJSONString(parMap), Conclusion.FAIL , errorMsg , ret);
			return retObject;
		}
		
		
	}


	@Override
	public JSONObject queryTotal() {
		String ret = null;
		JSONObject retObject = new JSONObject();
		String sign = MD5.passmd5("key=5359fd245e3fe71c0dee8ed8788f0b9d");
		Map<String,Object> parMap = new HashMap<String,Object>();
		parMap.put("sign", sign);
		try {
			ret = HttpUtil.get(appgame_total_url, parMap);
			if(StringUtils.isBlank(ret)) {
				throw new BaseServiceException("错误, APPGAME接口返回值为空");
			}
			if(!JSONUtil.isJson(ret)) {
				throw new BaseServiceException("错误, APPGAME接口返回值格式不是一个有效的JSON");
			}
			retObject.put("code", "1000");
			return JSONObject.parseObject(ret);
		} catch (Exception e) {
			LOGGER.error("请求APP游戏服务器发生错误， 详情: {} , {}" , e.getMessage() , e);
			String errorMsg = e.getMessage();
			if(StringUtils.isNotBlank(e.getMessage()) && e.getMessage().length() > 150) {
				errorMsg = StringUtils.substring(e.getMessage(), 0, 150);
			}
			retObject.put("code", "9999");
			retObject.put("msg", errorMsg);
			apiLogService.addApiLog(LogType.REQUEST_APP_GANE_TOTAL, JSONObject.toJSONString(parMap), Conclusion.FAIL , errorMsg , ret);
			return retObject;
		}
	}
	
	

}
